package com.xyu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xyu.gulimall.product.entity.ProductAttrValueEntity;
import com.xyu.gulimall.product.service.ProductAttrValueService;
import com.xyu.gulimall.product.vo.AttrRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xyu.gulimall.product.entity.AttrEntity;
import com.xyu.gulimall.product.service.AttrService;
import com.xyu.common.utils.PageUtils;
import com.xyu.common.utils.R;


/**
 * 商品属性
 *
 * @author wangyufei
 * @email 2070522113@qq.com
 * @date 2021-05-03 21:27:00
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    //    @RequestMapping("/base/list/{catelogId}")
//    //@RequiresPermissions("product:attr:list")
//    public R BaseAttrlist(@RequestParam Map<String, Object> params,
//                          @PathVariable("catelogId") Long catelogId){
//        System.out.println("执行");
//        PageUtils page = attrService.queryBaseAttrPate(params,catelogId);
//        List<?> list = page.getList();
//        System.out.println("输出");
//         list.forEach(System.out::println);
//        return R.ok().put("page", page);
//    }
    /**
     * 参数 parms分页信息
     * catelogId查询id
     * type查询类型
     */
    //pms_attr和关联关系表商品表结果查询
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     * 增强回显信息[修改回显]
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrRespVo attrRespVo) {
        attrService.saveAttr(attrRespVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrEntity attr) {
        attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    /**
     * 获取spu规格表 pms_product_attr_value
     * @param spuId
     * @return
     * 请求地址  /product/attr/base/listforspu/{spuId}
     */

    @GetMapping("/base/listforspu/{spuId}")
    public R ListSpu(@PathVariable("spuId") Long spuId){
        //获取spu规格
        List<ProductAttrValueEntity> productAttr= productAttrValueService.ProductAttr(spuId);
        return R.ok().put("data",productAttr);
    }
    /**
     * 修改商品规格 表 pms_product_attr_value
     * 请求地址：/product/attr/update/{spuId}
     * @param entities
     * @param spuId
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R UpdateSpu(@RequestBody List<ProductAttrValueEntity> entities,@PathVariable("spuId") Long spuId){
        productAttrValueService.UpdateSpuBatch(entities,spuId);
        return R.ok();
    }
}
